using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Microsoft.Xna.Framework;

namespace GameFramework.Extensions
{
    /// <summary>
    /// Binary Writer Extension Methods.
    /// </summary>
    [SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented", Justification = "Reviewed. Suppression is OK here.")]
    public static class BinaryWriterExtensions
    {
        #region Value Types

        public static void WriteInt(this BinaryWriter writer, int value)
        {
            writer.Write(value);
        }

        public static void WriteFloat(this BinaryWriter writer, float value)
        {
            writer.Write(value);
        }

        public static void WriteRectangle(this BinaryWriter writer, Rectangle value)
        {
            writer.Write(value.X);
            writer.Write(value.Y);
            writer.Write(value.Width);
            writer.Write(value.Height);
        }

        public static void WriteColor(this BinaryWriter writer, Color value)
        {
            writer.WriteVector4(value.ToVector4());
        }

        public static void WriteVector2(this BinaryWriter writer, Vector2 value)
        {
            writer.Write(value.X);
            writer.Write(value.Y);
        }

        public static void WriteVector3(this BinaryWriter writer, Vector3 value)
        {
            writer.Write(value.X);
            writer.Write(value.Y);
            writer.Write(value.Z);
        }

        public static void WriteVector4(this BinaryWriter writer, Vector4 value)
        {
            writer.Write(value.X);
            writer.Write(value.Y);
            writer.Write(value.Z);
            writer.Write(value.W);
        }

        public static void WriteDateTime(this BinaryWriter writer, DateTime value)
        {
            writer.Write(value.Ticks);
        }

        public static void WriteTimeSpan(this BinaryWriter writer, TimeSpan value)
        {
            writer.Write(value.Ticks);
        }

        #endregion

        #region Reference Types

        public static void WriteString(this BinaryWriter writer, string value)
        {
            writer.Write(value ?? string.Empty);
        }

        public static void WriteObject<T>(this BinaryWriter writer, T value) where T : class, IBinarySerializable
        {
            if (value != null)
            {
                writer.Write(true);
                value.Write(writer);
            }
            else
            {
                writer.Write(false);
            }
        }

        #endregion

        #region List of Reference Types (Except Strings)

        public static void WriteList<T>(this BinaryWriter writer, List<T> list) where T : IBinarySerializable
        {
            if (list != null)
            {
                writer.Write(list.Count);

                foreach (T item in list)
                {
                    item.Write(writer);
                }
            }
            else
            {
                writer.Write(0);
            }
        }

        #endregion

        #region List of Strings and Value Types

        public static void WriteStringList(this BinaryWriter writer, List<string> list)
        {
            if (list != null)
            {
                writer.Write(list.Count);

                foreach (string item in list)
                {
                    writer.Write(item);
                }
            }
            else
            {
                writer.Write(0);
            }
        }

        #endregion

        #region Array of Reference Types

        public static void WriteArray<T>(this BinaryWriter writer, T[] array) where T : IBinarySerializable
        {
            if (array != null)
            {
                writer.Write(array.Length);

                foreach (T item in array)
                {
                    item.Write(writer);
                }
            }
            else
            {
                writer.Write(0);
            }
        }

        #endregion

        #region Array of Value Types

        public static void WriteFloatArray(this BinaryWriter writer, float[] array)
        {
            if (array != null)
            {
                writer.WriteInt(array.Length);

                foreach (float item in array)
                {
                    writer.WriteFloat(item);
                }
            }
            else
            {
                writer.WriteInt(0);
            }
        }

        public static void WriteVector2Array(this BinaryWriter writer, Vector2[] array)
        {
            if (array != null)
            {
                writer.WriteInt(array.Length);

                foreach (Vector2 item in array)
                {
                    writer.WriteVector2(item);
                }
            }
            else
            {
                writer.Write(0);
            }
        }

        #endregion
    }
}